<?php
Class Booking extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
		$this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'booking';
        $this->load->model('Mbooking');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 16, $this->session->userdata('admin_id'));
        
    }
    
    public function index()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['arrLabel'] = $this->Mbooking->getListLabel();
        $this->data['arrZone'] = $this->Mbooking->getListZone();
        
        @$this->data['label_id'] = $this->getParamInt('label_id');
        @$this->data['zone_id'] = $this->getParamInt('zone_id');
        @$this->data['time'] = $this->getParamString('time');
        
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] = 20;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mbooking->getBookingDB($config['per_page'],$offset,@$this->data['label_id'],@$this->data['zone_id'],@$this->data['time']);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $config['paramhack'] = '';
        if(!in_array($this->data['label_id'],array(null, '', '0', 0))){
            $config['paramhack'] .= '?label_id='.$this->data['label_id'];
        }
        if(!in_array($this->data['zone_id'],array(null, '', '0', 0))){
            if(!in_array($this->data['label_id'],array(null, '', '0', 0))){
                $config['paramhack'] .= '&zone_id='.$this->data['zone_id'];
            }else{
                $config['paramhack'] .= '?zone_id='.$this->data['zone_id'];
            }
        }
        if(!in_array($this->data['time'],array(null, '', '0', 0))){
            if(!in_array($this->data['label_id'],array(null, '', '0', 0)) || !in_array($this->data['zone_id'],array(null, '', '0', 0))){
                $config['paramhack'] .= '&time='.$this->data['time'];
            }else{
                $config['paramhack'] .= '?time='.$this->data['time'];   
            }
        }
        $this->data['thisParam'] = $config['paramhack'];
        $this->pagination->initialize($config);
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data;
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('label_id', 'Nhãn lọc', '');
        if($this->form_validation->run() === TRUE)
        {
            /*$idArray = $this->getParamArray('checkboxs');
            $run = $this->Mbooking->deleteBookingDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>alert("Xóa thành công");window.location="'.$this->data['baseurl'].'manage/booking/index";</script>');
            }*/
            
            $this->data['label_id'] = $this->getParamInt('label_id');
            $this->data['zone_id'] = $this->getParamInt('zone_id');
            $this->data['time'] = $this->getParamString('time');
            //die($this->data['label_id'].'---'.$this->data['zone_id'].'--'.$this->data['time']);
            
            $this->data['id_active'] = $this->getParamUri(4,3);
            $this->load->library('pagination');
            $config['per_page'] = 20;
            $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
            $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
            $data = $this->Mbooking->getBookingDB($config['per_page'],$offset,$this->data['label_id'],$this->data['zone_id'],$this->data['time']);
            $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
            $config['total_rows'] = $data['count'];
            $config['uri_segment'] = 4;
            $config['uri_segment_page'] = $this->getParamUri(4,3);
            $config['suffix'] = '.html';
            $config['paramhack'] = '';
            if(!in_array($this->data['label_id'],array(null, '', '0', 0))){
                $config['paramhack'] .= '?label_id='.$this->data['label_id'];
            }
            if(!in_array($this->data['zone_id'],array(null, '', '0', 0))){
                if(!in_array($this->data['label_id'],array(null, '', '0', 0))){
                    $config['paramhack'] .= '&zone_id='.$this->data['zone_id'];
                }else{
                    $config['paramhack'] .= '?zone_id='.$this->data['zone_id'];
                }
            }
            if(!in_array($this->data['time'],array(null, '', '0', 0))){
                if(!in_array($this->data['label_id'],array(null, '', '0', 0)) || !in_array($this->data['zone_id'],array(null, '', '0', 0))){
                    $config['paramhack'] .= '&time='.$this->data['time'];
                }else{
                    $config['paramhack'] .= '?time='.$this->data['time'];   
                }
            }
            $this->data['thisParam'] = $config['paramhack'];
            $this->pagination->initialize($config);
            
            $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
            $this->data['data'] = $data;
        }
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
	
    public function editbooking()
    {
		
		if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
	
        $this->load->library('form_validation');
		$this->form_validation->set_rules('restaurant_id', 'Nhà hàng', 'required');  
		$this->form_validation->set_rules('address_id', 'Địa chỉ', 'required');
		$this->form_validation->set_rules('name', 'Họ tên', 'required');
        $this->form_validation->set_rules('phone', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('email', 'Địa chỉ email', 'required');
		$this->form_validation->set_rules('peoples', 'Số người', 'required');
        $this->form_validation->set_rules('required', 'Yêu cầu', 'required');
		
		$id = $this->getParamUri(4,1);
		$data = $this->Mbooking->getBookingInfo($id);
	
		$this->data['getBookingInfo'] = $data;
		$this->data['optionRestaurant'] = $this->Mbooking->optionRestaurant($data->rid);
        $this->data['optionAddress'] = $this->Mbooking->optionAddress($data->raid);
		$this->data['optionStar'] = $this->Mbooking->optionStar($data->star);
		
        if($this->form_validation->run() === TRUE)
        {
            $restaurant_id = $this->getParamInt('restaurant_id');
			$address_id = $this->getParamInt('address_id');
			$name = $this->getParamString('name');
			$phone = $this->getParamString('phone');
			$email = $this->getParamString('email');
			$dates = $this->getParamString('dates'); $arr_dates = explode("/",$dates); 
			$dates = $arr_dates[1]."/".$arr_dates[0]."/".$arr_dates[2];
			$hours = $this->getParamString('hours');
			$time_booking = $hours."|".$dates;
			$peoples = $this->getParamInt('peoples');
			$required = $this->getParamString('required');
			$improved = $this->getParamString('improved'); 
			if( $improved == TRUE || $improved == 'on' ){ $improved = 1;}else{ $improved = 0;}
            $this->data['data'] = $this->Mbooking->editBooking($id,$restaurant_id,$address_id,$name,$phone,$email,$time_booking,$peoples,$required,$improved);
			if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa thành công");window.location.href="'.$this->data['baseurl'].'manage/booking/edit-booking/'.$id.'";</script>');
            }
        }
		
		$this->data['action'] = 'editbooking';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deletebooking()
    {
	
		if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
	
        $id = $this->getParamUri(4,1);
        $run = $this->Mbooking->deleteBookingDb($id);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/booking/index";</script>');
        }
    }
    
    public function report()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        @$this->data['label_id'] = $this->getParamInt('label_id');
        if(!in_array(@$this->data['label_id'], array(null, '', '0', 0))){
            $this->data['label'] = $this->Mbooking->getreportLabel($this->data['label_id']);
        }
        @$this->data['zone_id'] = $this->getParamInt('zone_id');
        if(!in_array(@$this->data['zone_id'], array(null, '', '0', 0))){
            $this->data['zone'] = $this->Mbooking->getreportZone($this->data['zone_id']);
        }
        @$this->data['time'] = $this->getParamString('time');
        
        $this->data['data'] = $this->Mbooking->reportData(@$this->data['label_id'], @$this->data['zone_id'], @$this->data['time']);
        
        $this->data['action'] = 'report';
        $this->load->view('template/booking/report', $this->data);
    } 
    
}